import { unicomSelect,unicomProtocal,unicomChange } from "../../../api";
Page({
  data: {
    value: '',   //输入框内容
    list:[],
    itemIndex:-1,
    certId:'',
    certName:'',
    goodsId:'',
    id:'',
    checked:'',
    isAgree: false,
    phoneNum:'',
    status:0
  },
  onLoad() {
    this.getNumber(this.data.value)
  },
  handleInput(value) {
    this.setData({
      value,
    });
  },
  handleClear() {
    this.setData({
      value: '',
    });
  },
  checkNumber(e){
    console.log(e);
    let itemIndex=e.currentTarget.dataset.itemIndex
    if(this.data.itemIndex == itemIndex){
      this.setData({
        itemIndex: -1,
        
      })
    }else{
      this.setData({
        itemIndex: itemIndex,
        checked:itemIndex,
        phoneNum:e.currentTarget.dataset.value
      })
    }
    console.log(this.data.checked);
  },
  serchBtn(e){
    console.log(e);
    my.showLoading({
      content: '加载中...',
      complete:(res=>{
        setTimeout(() => {
          my.hideLoading({})
        }, 2000)
      })
    });
    this.getNumber(e.currentTarget.dataset.index)
  },
  changeAgree(){
    this.setData({isAgree: !this.data.isAgree,status:1})
  },
  getUnicom(e){
    my.navigateTo({ url: '../../../packageB/pages/financing/financing?id='+e.currentTarget.dataset.id})
    // console.log(e);
  },
  handleChange(e){
    my.showLoading({
      content: '加载中...',
      complete:(res=>{
        setTimeout(() => {
          my.hideLoading({})
        }, 2000)
      })
    });
    this.getNumber(e.currentTarget.dataset.index)
  },
  getNumber(searchValue){
    unicomSelect(searchValue).then(res=>{
      this.setData({
        list:res,
        certId:res.certId,
        certName:res.certName,
        goodsId:res.goodsId,
        id:res.id
      })
      console.log(this.data.certName);
    })
  },
  goUnicom(){
    let obj={
      status:this.data.status,
      certNo:this.data.certId,
      certName:this.data.certName,
      phoneNum:this.data.phoneNum,
      id:this.data.id
    }
    if(this.data.status==0){
      my.showToast({
        type:'error',
        content:'请勾选同意并签署协议！',
        duration:2000
      })
    }else{
      if(obj.phoneNum==''){
        my.showToast({
          type:'error',
          content:'请先选择手机号码',
          duration:2000
        })
      }else{
        unicomChange(obj).then(res=>{
          my.showToast({
            type:'success',
            content:'选号成功！',
            duration:2000
          })
        })
      }
    }
  }
});
